<?php
/**
 * Created by PhpStorm.
 * User: 郑好
 * Date: 2019/6/28
 * Time: 下午2:08
 */

namespace app\common\model;

use app\common\InstanceTrait;
use think\Db;
use think\Exception;
use Log;

class AdminOpLogModel
{
    use InstanceTrait;

    /**
     * @param       $data
     * @return      array
     * @description 新增操作记录
     */
    public function insert($data)
    {
        try {
            $isInsert = DB::table("admin_op_log")->insert(
                [
                "admin_id" => $data["admin_id"],
                "employee_no"=> $data["employee_no"],
                "op_type"=> $data["op_type"],
                "op_object"=> $data["op_object"],
                "op_info"=> $data["op_info"],
                "op_status"=> $data["op_status"],
                "op_ip" => $data["op_ip"],
                "op_time" => time(),
                "sys" => $data["sys"],
                ]
            );

            if (!$isInsert) {
                return ["code" => _MSG_INVALID_CLIENT_PARAM, "msg" => "添加操作记录失败", "data" => []];
            }

            return ["code" => _MSG_SYSTEM_SUCCESS, "msg" => "添加操作记录成功", "data" => []];
        } catch (Exception $e) {
            \Log::error("添加操作记录失败" . $e->getMessage(), []);

            // 这是进行异常捕获
            return ["code" => _MSG_SYSTEM_ERROR, "msg" => "添加操作记录失败", "data" => []];
        }
    }

    public function listByPager($data)
    {
        $page = $data["page_no"];
        $pageCount = $data["page_count"];

        $op = Db::table("admin_op_log")
            ->alias("log")
            ->leftJoin("admin", "admin.admin_id = log.admin_id");
        if (isset($data["admin_id"]) && (int)$data["admin_id"]>0) {
            $op->where("log.admin_id", $data["admin_id"]);
        }
        if (isset($data["sys"]) && !empty($data["sys"])) {
            $op->where("log.sys", $data["sys"]);
        }
        if (isset($data["admin_name"]) && !empty($data["admin_name"])) {
            $op->where("( admin.name LIKE '%" . $data["admin_name"] . "%' )");
        }
        if ($data['lifting'] == 1 && $data['field'] == "op_time") {
            $count = $op->count("op_time");
        } elseif ($data['lifting'] == 0 && $data['field'] == "op_time") {
            $count = $op->count("op_time");
        } else {
            $count = $op->count();
        }
        $list = [];
        if ($count >= 0) {
            $op = Db::table("admin_op_log")
                ->alias("log")
                ->leftJoin("admin", "admin.admin_id = log.admin_id")
                ->field("log.admin_id,admin.name as admin_name,log.employee_no,log.op_time,log.op_type,log.op_object,log.op_info,log.op_status,log.op_ip");
            if (isset($data["admin_id"]) && (int)$data["admin_id"]>0) {
                $op->where("log.admin_id", $data["admin_id"]);
            }
            if (isset($data["sys"]) && !empty($data["sys"])) {
                $op->where("log.sys", $data["sys"]);
            }
            if (isset($data["admin_name"]) && !empty($data["admin_name"])) {
                $op->where("( admin.name LIKE '%" . $data["admin_name"] . "%' )");
            }
            if ($data['lifting'] == 1 && $data['field'] == "op_time") {
                $list = $op->order("op_time ASC")
                    ->page($page, $pageCount)
                    ->select();
            } elseif ($data['lifting'] == 0 && $data['field'] == "op_time") {
                $list = $op->order("op_time DESC")
                    ->page($page, $pageCount)
                    ->select();
            } else {
                $list = $op->order("op_time DESC")
                    ->page($page, $pageCount)
                    ->select();
            }
        }

        $pageInfo = [
            "total" => $count,
            "page_no" => $page,
            "page_count" => count($list)
        ];

        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]["op_info"] = json_decode($v["op_info"], true);
            }
        }
        return ["code"=>_MSG_SYSTEM_SUCCESS, "msg"=>"获取操作记录成功", "data"=> ["page_info"=>$pageInfo, "list" => $list]];
    }
}
